<?php
/**
 * 案例
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/6/2
 * Time: 11:57
 */

namespace app\admin\controller;
use app\admin\model\CasesCat;
use think\Db;
use think\Loader;
use think\Request;

class Cases extends Admin
{
    public function lst($keyword = '')
    {
        $article_model =  model('cases');
        $data = $article_model->search(trim($keyword));
        $this->assign("keyword",input('keyword'));
        return $this->fetch('',$data);
    }


    /**
     * 添加案例
     * 属于多对多关系
     */
    public function add()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('Cases');
            $cases = input('post.');
            if($validate->check($cases))
            {
                $cases['add_time'] = strtotime($cases['add_time'])?:time();
                $casesModel = new \app\admin\model\Cases();
                $data = $casesModel->upload($cases);
                $case = array_merge($cases,$data);
                $cat_id = $case['cat_id'];
                unset($case['cat_id']);
                Db::name('cases')->insert($case);
                $id = Db::name('cases')->getLastInsID();
                $list = [];
                foreach ($cat_id as $v)
                {
                    $list[] = ['case_id'=>$id,'case_cat_id'=>$v];
                }
                $res = db('case_middle')->insertAll($list); //插入中间表
                if($res)
                {
                    $this->success('添加成功', url('lst'));
                }
                else
                {
                    $this->success('添加失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        //取出分类
        $cases_cat =  db('cases_cat')->field('cat_id,cat_name,parent_id')->select();
        $casesCat = new CasesCat();
        $data['cases_cat'] = $casesCat->getChildArr($cases_cat);
        return $this->fetch('',$data);
    }


    /**
     * 修改案例
     * @param $id
     */
    public function save($id)
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('Cases');
            $cases = input('post.');
            if($validate->check($cases))
            {
                $cases['add_time'] = strtotime($cases['add_time'])?:time();
                $casesModel = new \app\admin\model\Cases();
                $data = $casesModel->upload($cases);
                $case = array_merge($cases,$data);
                $cat_id = $case['cat_id'];
                unset($case['cat_id']);
                unset($case['old_img']);
                Db::name('cases')->update($case);
                //删除中间表的老数据
                db('case_middle')->where(['case_id'=>$id])->delete();
                $list = [];
                foreach ($cat_id as $v)
                {
                    $list[] = ['case_id'=>$id,'case_cat_id'=>$v];
                }
                $res = db('case_middle')->insertAll($list); //插入中间表
                if($res)
                {
                    $this->success('修改成功', url('lst'));
                }
                else
                {
                    $this->success('修改失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        //案例基本信息
        $data['caseInfo'] = db('cases')->find($id);
        //中间表关联的信息
        $caseMiddle = db('case_middle')->where(['case_id'=>$id])->select();
        foreach ($caseMiddle as $k=>$v)
        {
            $data['case_select'][] = $v['case_cat_id'];
        }
        //分类
        $cases_cat =  db('cases_cat')->field('cat_id,cat_name,parent_id')->select();
        $casesCat = new CasesCat();
        $data['cases_cat'] = $casesCat->getChildArr($cases_cat);
        return $this->fetch('',$data);
    }

    /**
     * 删除案例
     * @param $id
     */
    public function delete($id)
    {
        $cases_model = db('cases');
        $case_data = $cases_model->where(array('id'=>$id))->find();
        if($case_data)
        {
            //删除原图与缩略图
            $data[] = ROOT_PATH.'public'.$case_data['original_img'];
            $data[] = ROOT_PATH.'public'.$case_data['thumb2_img'];
            $data[] = ROOT_PATH.'public'.$case_data['thumb1_img'];
            foreach ($data as $k=>$v)
            {
                if(is_file($v))
                {
                    unlink($v);
                }
            }
            $cases_model->delete($id);
            db('case_middle')->where(['case_id'=>$id])->delete();
            $this->success("删除成功！");
        }
        else
        {
            $this->error("手贱，滚蛋吧！");
        }
    }



}